package com.hjq.demo.yuliao.fragment

import android.content.res.ColorStateList
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.gyf.immersionbar.ImmersionBar
import com.hjq.base.BaseDialog
import com.hjq.base.FragmentPagerAdapter
import com.hjq.demo.R
import com.hjq.demo.app.vb.TitleBarVbFragment
import com.hjq.demo.databinding.FragmentHomeMainBinding
import com.hjq.demo.http.model.HttpData
import com.hjq.demo.ui.adapter.TabAdapter
import com.hjq.demo.ui.adapter.TabAdapter.OnTabListener
import com.hjq.demo.ui.fragment.BrowserFragment
import com.hjq.demo.ui.fragment.StatusFragment
import com.hjq.demo.utils.CommonSpaceItemDecoration
import com.hjq.demo.widget.XCollapsingToolbarLayout
import com.hjq.demo.widget.XCollapsingToolbarLayout.OnScrimsListener
import com.hjq.demo.yuliao.MainActivity
import com.hjq.demo.yuliao.adapter.HomeMainRecommendAdapter
import com.hjq.demo.yuliao.api.GetRoomBgListApi
import com.hjq.demo.yuliao.dialog.OpenRoomDialog
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback

/**
 *
 * @author jiahe
 * 创建日期 2022/2/16
 * 描述 首页
 *
 */
class HomeMainFragment : TitleBarVbFragment<MainActivity, FragmentHomeMainBinding>(), OnTabListener,
    OnPageChangeListener, OnScrimsListener {

    companion object {

        fun newInstance(): HomeMainFragment {
            return HomeMainFragment()
        }
    }

    private val collapsingToolbarLayout: XCollapsingToolbarLayout? by lazy { findViewById(R.id.ctl_home_bar) }
    private val toolbar: Toolbar? by lazy { findViewById(R.id.tb_home_title) }
    private val addressView: TextView? by lazy { findViewById(R.id.tv_home_address) }
    private val hintView: TextView? by lazy { findViewById(R.id.tv_home_hint) }

    //    private val searchView: AppCompatImageView? by lazy { findViewById(R.id.iv_home_search) }
    private val tabView: RecyclerView? by lazy { findViewById(R.id.rv_home_tab) }
    private val viewPager: ViewPager? by lazy { findViewById(R.id.vp_home_pager) }

    private var tabAdapter: TabAdapter? = null
    private var pagerAdapter: FragmentPagerAdapter<Fragment>? = null

    private var mRecommendAdapter: HomeMainRecommendAdapter? = null

    override fun initView() {
        pagerAdapter = FragmentPagerAdapter(this)
        pagerAdapter!!.addFragment(HomeMainRoomListFragment.newInstance(), "列表演示")
        pagerAdapter!!.addFragment(HomeMainRoomListFragment.newInstance(), "网页演示")
        viewPager?.adapter = pagerAdapter
        viewPager?.addOnPageChangeListener(this)
        tabAdapter = getAttachActivity()?.let { TabAdapter(it, TabAdapter.TAB_MODE_DESIGN, false) }
        tabView?.adapter = tabAdapter

        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐
        ImmersionBar.setTitleBar(getAttachActivity(), toolbar)

        // 设置渐变监听
        collapsingToolbarLayout?.setOnScrimsListener(this)

        // 推荐
        mRecommendAdapter = HomeMainRecommendAdapter()
        binding.recyclerRecommend.layoutManager = LinearLayoutManager(activity, RecyclerView.HORIZONTAL, false)
        binding.recyclerRecommend.addItemDecoration(
            CommonSpaceItemDecoration(
                resources.getDimension(R.dimen.dp_10).toInt(),
                false, CommonSpaceItemDecoration.LINEARLAYOUT, true
            )
        )
        binding.recyclerRecommend.adapter = mRecommendAdapter

        val mList = mutableListOf<String>()
        mList.add("123")
        mList.add("123")
        mList.add("123")
        mList.add("456")
        mList.add("456")
        mList.add("456")
        mList.add("456")
        mRecommendAdapter?.setNewInstance(mList)

        setOnClickListener(binding.dtvHomeRoom)
    }

    override fun initData() {
        tabAdapter?.addItem("人气房间")
        tabAdapter?.addItem("房间分类")
        tabAdapter?.setOnTabListener(this)
    }

    override fun isStatusBarEnabled(): Boolean {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled()
    }

    override fun isStatusBarDarkFont(): Boolean {
        return collapsingToolbarLayout?.isScrimsShown() == true
    }

    /**
     * [TabAdapter.OnTabListener]
     */
    override fun onTabSelected(recyclerView: RecyclerView?, position: Int): Boolean {
        viewPager?.currentItem = position
        return true
    }

    /**
     * [ViewPager.OnPageChangeListener]
     */
    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

    override fun onPageSelected(position: Int) {
        tabAdapter?.setSelectedPosition(position)
    }

    override fun onPageScrollStateChanged(state: Int) {}

    /**
     * CollapsingToolbarLayout 渐变回调
     *
     * [XCollapsingToolbarLayout.OnScrimsListener]
     */
    @Suppress("RestrictedApi")
    override fun onScrimsStateChange(layout: XCollapsingToolbarLayout?, shown: Boolean) {
        getStatusBarConfig().statusBarDarkFont(shown).init()
        addressView?.setTextColor(
            ContextCompat.getColor(
                getAttachActivity()!!,
                if (shown) R.color.black else R.color.white
            )
        )
        hintView?.setBackgroundResource(if (shown) R.drawable.home_search_bar_gray_bg else R.drawable.home_search_bar_transparent_bg)

        hintView?.setTextColor(
            ContextCompat.getColor(
                getAttachActivity()!!,
                if (shown) R.color.black60 else R.color.black60
            )
        )

//        hintView?.setTextColor(
//            ContextCompat.getColor(
//                getAttachActivity()!!,
//                if (shown) R.color.black60 else R.color.white60
//            )
//        )
//        searchView?.supportImageTintList = ColorStateList.valueOf(
//            ContextCompat.getColor(
//                getAttachActivity()!!,
//                if (shown) R.color.common_icon_color else R.color.white
//            )
//        )
    }

    override fun onDestroy() {
        super.onDestroy()
        viewPager?.adapter = null
        viewPager?.removeOnPageChangeListener(this)
        tabAdapter?.setOnTabListener(null)
    }

    override fun onClick(view: View) {
        super.onClick(view)

        if (view == binding.dtvHomeRoom) {

            EasyHttp.post(this)
                .api(GetRoomBgListApi())
                .request(object : HttpCallback<HttpData<GetRoomBgListApi.RoomBgListResp?>>(this) {
                    override fun onSucceed(result: HttpData<GetRoomBgListApi.RoomBgListResp?>) {
                        super.onSucceed(result)

                        val mList = mutableListOf<Any>()
                        mList.add("私密")
                        mList.add("公开")

                        var mListBg = mutableListOf<Any>()
                        if (result.getData()?.list!=null){
                            mListBg = result.getData()?.list?.toMutableList()!!
                        }

                        getAttachActivity()?.let {
                            OpenRoomDialog.Builder(it)
                                .setList(mList)
                                .setBgList(mListBg)
                                // 设置单选模式
                                .setSingleSelect()
                                // 设置默认选中
                                .setSelect(0)
                                .setListener(object : OpenRoomDialog.OnListener<String> {

                                    override fun onSelected(
                                        dialog: BaseDialog?,
                                        data: HashMap<Int, String>,
                                        dataBg: HashMap<Int, String>
                                    ) {

                                    }

                                })
                                .show()
                        }
                    }
                })


        }
    }
}